package Gui;

import java.util.HashMap;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import tings.Moves;
import LBC.CollisionListener;
import Objects.Body;
import Objects.BodyCircle;
import Objects.BodyRectangle;
 
public class GameplayState extends BasicGameState {
 
    int stateID = 0;
	@SuppressWarnings("unchecked")
	public HashMap []hM;
	public HashMap<Integer, Body> bodies;
    public HashMap<Body, BodyRectangle> btr;
    public HashMap<Body, BodyCircle> btc;
	CollisionListener listener;
	Moves mover;
	Integer time;
	boolean pause=false;
 
    @SuppressWarnings("unchecked")
	public GameplayState(int stateID, HashMap[] hM ) 
    {
       this.stateID = stateID;
       this.hM=hM;
       bodies=hM[0];
       btr=hM[1];
       btc=hM[2];
    }
 
    @Override
    public int getID() {
        return 2;
    }
 
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    	gc.setTargetFrameRate(60);
		initialization();
    }
 
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
    	//poustawiac kolory obiektow  i je powypelniac po kolei
    	for(int i=0; i<bodies.size(); i++){
    		g.setColor(bodies.get(i).color);
    		g.fill(bodies.get(i).shape);
    	}
    }
 
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    	Input in=gc.getInput();
    	
    	if(in.isKeyDown(Input.KEY_R)){
            initialization();
		}
    	if(in.isKeyDown(Input.KEY_P)){
            pause=true;
		}
    	if(in.isKeyDown(Input.KEY_SPACE)){
            pause=false;
		}
    	
		if(!pause)
		{
			mover.move();
			listener.bodyChecker2();
			time++;
		}
    }
    
    private void initialization(){
		time=1;
		//TODO BIG BUFFER with values to restart
//		shapes=new ArrayList<Shape>();
//		stc=new HashMap<Shape, BodyCircle>();  //pobrac z pliku? z pliku zapisanego w setobjectsstate/toolbar
//		str=new HashMap<Shape, BodyRectangle>();
//		stb=new HashMap<Shape, Body>();
		listener=new CollisionListener(hM,time);
		mover=new Moves(hM,time);
//		addRectangle();		<-wymyslic cos co doda po kolei wszystkie prostokaty i kolka 
//		addCircle();			
    }
    
    public void addCircle(BodyCircle c){
		bodies.put(bodies.size()-1, c);
		btc.put(bodies.get(hM.length-1), c);
		bodies.get(bodies.size()-1).resetColXY();
		c.addColisionListener(listener);
	}
    
    public void addRectangle(BodyRectangle r){
		bodies.put(bodies.size()-1, r);
		btr.put(bodies.get(hM.length-1), r);
		bodies.get(bodies.size()-1).resetColXY();
		r.addColisionListener(listener);
	}
 
}
